package zl.ordermanager.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import zl.ordermanager.service.PurchaseInvoiceService;
import zl.ordermanager.service.UserService;
import zl.ordermanager.vo.DataGrid;
import zl.ordermanager.vo.EInvoiceMethod;
import zl.ordermanager.vo.EInvoiceType;
import zl.ordermanager.vo.PageHelper;
import zl.ordermanager.vo.PurchaseInvoice;
import zl.ordermanager.vo.Total;

@Controller
@RequestMapping("/purchaseInvoice")
public class PurchaseInvoiceController extends BaseController {

	@Autowired
	private PurchaseInvoiceService purchaseInvoiceService;

	@Autowired
	private UserService userService;

	@RequestMapping("/showList")
	public String showList(PageHelper pageHelper, Model model) {
		Total total = new Total();
		DataGrid<PurchaseInvoice> dataGrid = purchaseInvoiceService.dataGrid(pageHelper, total);
		List<PurchaseInvoice> purchaseInvoiceList = dataGrid.getList();
		long totalCount = dataGrid.getTotal();

		model.addAttribute("purchaseInvoiceList", purchaseInvoiceList);
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageHelper", pageHelper);
		model.addAttribute("total", total);

		return "purchaseInvoiceList";
	}

	@RequestMapping("/showAdd")
	public String showAdd(Model model) {
		model.addAttribute("invoiceTypeList", EInvoiceType.values());
		model.addAttribute("invoiceMethodList", EInvoiceMethod.values());
		model.addAttribute("currentMonth", new SimpleDateFormat("yyyy-M").format(new Date()));

		return "purchaseInvoiceAdd";
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public ModelAndView add(PurchaseInvoice purchaseInvoice) {
		try {
			purchaseInvoiceService.add(purchaseInvoice);
		} catch (Exception e) {
			return ajaxDoneError(e.getMessage());
		}

		return ajaxDoneSuccess("操作成功");
	}

	@RequestMapping("/showEdit/{InvoiceId}")
	public String showEdit(@PathVariable("InvoiceId") Integer InvoiceId, Model model) {
		PurchaseInvoice purchaseInvoice = purchaseInvoiceService.get(InvoiceId);

		model.addAttribute("purchaseInvoice", purchaseInvoice);
		model.addAttribute("invoiceTypeList", EInvoiceType.values());
		model.addAttribute("invoiceMethodList", EInvoiceMethod.values());

		return "purchaseInvoiceEdit";
	}

	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	public ModelAndView edit(PurchaseInvoice purchaseInvoice) {
		try {
			purchaseInvoiceService.edit(purchaseInvoice);
		} catch (Exception e) {
			return ajaxDoneError(e.getMessage());
		}
		return ajaxDoneSuccess("操作成功");
	}

	@RequestMapping("/delete/{InvoiceId}")
	public ModelAndView delete(@PathVariable("InvoiceId") Integer InvoiceId) {

		purchaseInvoiceService.delete(InvoiceId);

		return ajaxDoneSuccess("操作成功");
	}

}
